import csv
import json

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')

@app.route('/sales_month')
def sales_month():
    data = []
    # TODO 读取data/sales_month.csv中的各月销售数据，提供给浏览器端调用
    with open('data/sales_month.csv', 'r', encoding='utf8') as f:
        read=csv.reader(f)
        next(read)
        for row in read:
            data.append({'month':row[0],'sales':row[1],'profit':row[2]})
    return {'data': data}

@app.route('/sales_product')
def sales_product():
    with open('data/sales_product.json', 'r', encoding='utf8') as f:
        data=json.load(f)
    return {'data': data}

@app.route('/sales_region')
def sales_region():
    data=[]
    with open('data/sales_region.csv','r',encoding='utf8') as f:
        red=csv.reader(f)
        next(red)
        for row in red:
            data.append({'region':row[0],'sales':row[1],'profit':row[2]})
    return {'data':data}

@app.route('/sales_manager')
def sales_manager():
    data=[]
    with open('data/sales_manager.csv','r',encoding='utf8') as f:
        re=csv.reader(f)
        next(re)
        for row in re:
            data.append({'sales_manager':row[0],'sales':row[1],'profit':row[2]})
    return {'data':data}

@app.route('/sales_province')
def sales_province():
    data=[]
    with open('data/sales_province.csv','r',encoding='utf8') as f:
        r=csv.reader(f)
        next(r)
        for row in r:
            data.append({'province':row[0],'sales':row[1],'profit':row[2]})
    return {'data':data}


if __name__ == '__main__':
    app.run()